package asia.lics.system.service.impl;

import asia.lics.common.exception.ServiceException;
import asia.lics.common.utils.DateUtils;
import asia.lics.system.domain.FSubject;
import asia.lics.system.mapper.FSubjectMapper;
import asia.lics.system.service.IFSubjectService;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 科目Service业务层处理
 *
 * @author ss-tutor
 * @date 2024-11-16
 */
@Service
public class FSubjectServiceImpl implements IFSubjectService {
    @Autowired
    private FSubjectMapper fSubjectMapper;

    /**
     * 查询科目
     *
     * @param id 科目主键
     * @return 科目
     */
    @Override
    public FSubject selectFSubjectById(Long id) {
        return fSubjectMapper.selectFSubjectById(id);
    }

    /**
     * 查询科目列表
     *
     * @param fSubject 科目
     * @return 科目
     */
    @Override
    public List<FSubject> selectFSubjectList(FSubject fSubject) {
        return fSubjectMapper.selectFSubjectList(fSubject);
    }

    /**
     * 新增科目
     *
     * @param fSubject 科目
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertFSubject(FSubject fSubject) {
        fSubject.setCreateTime(DateUtils.getNowDate());
        int insertFSubject = fSubjectMapper.insertFSubject(fSubject);
        String path = getPath(fSubject);
        fSubjectMapper.updateFSubjectPath(fSubject.getId(), path);
        return insertFSubject;
    }

    private String getPath(FSubject fSubject) {
        if (fSubject.getParentId() == 0L) {
            return 0 + "-" + fSubject.getId();
        }
        FSubject parent = fSubjectMapper.selectFSubjectById(fSubject.getParentId());
        return parent.getPath() + "-" + fSubject.getId();
    }

    /**
     * 修改科目
     *
     * @param fSubject 科目
     * @return 结果
     */
    @Override
    public int updateFSubject(FSubject fSubject) {
        fSubject.setUpdateTime(DateUtils.getNowDate());
        Long parentId = fSubject.getParentId();
        if (parentId != 0L) {
            FSubject parent = fSubjectMapper.selectFSubjectById(parentId);
            if (parent.getPath().contains(fSubject.getId() + "-")) {
                throw new ServiceException("不能将父节点设置为本节点的子节点");
            }
        }
        String path = getPath(fSubject);
        fSubject.setPath(path);
        return fSubjectMapper.updateFSubject(fSubject);
    }

    /**
     * 批量删除科目
     *
     * @param ids 需要删除的科目主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteFSubjectByIds(Long[] ids) {
        int i = fSubjectMapper.deleteFSubjectByIds(ids);
        deleteChild(ids);
        return i;
    }

    /**
     * 删除子科目信息
     */
    private void deleteChild(Long[] ids) {
        for (Long id : ids) {
            fSubjectMapper.deleteByPath(id);
        }
    }

    /**
     * 删除科目信息
     *
     * @param id 科目主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteFSubjectById(Long id) {
        Long[] ids = {id};
        deleteChild(ids);
        return fSubjectMapper.deleteFSubjectById(id);
    }

    @Override
    public List<Tree<Long>> selectFSubjectTree(String name) {
        List<FSubject> fSubjects = fSubjectMapper.selectFSubject(name);
        List<TreeNode<Long>> treeNodes = fSubjects.stream().map(fSubject -> {
            TreeNode<Long> node = new TreeNode<>();
            node.setId(fSubject.getId());
            node.setName(fSubject.getName());
            if (name != null) {
                node.setParentId(0L);
            } else {
                node.setParentId(fSubject.getParentId());
            }
            return node;
        }).collect(Collectors.toList());
        return TreeUtil.build(treeNodes, 0L);
    }
}
